//
//  SplashObj.m
//
//  Created by lizhenqiang  on 16/4/14
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "SplashObj.h"
#import "SplashChild.h"


NSString *const kSplashObjId = @"_id";
NSString *const kSplashObjLevel = @"level";
NSString *const kSplashObjName = @"name";
NSString *const kSplashObjIcon = @"icon";
NSString *const kSplashObjChild = @"child";


@interface SplashObj ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SplashObj

@synthesize internalBaseClassIdentifier = _internalBaseClassIdentifier;
@synthesize level = _level;
@synthesize name = _name;
@synthesize icon = _icon;
@synthesize child = _child;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.internalBaseClassIdentifier = [self objectOrNilForKey:kSplashObjId fromDictionary:dict];
            self.level = [[self objectOrNilForKey:kSplashObjLevel fromDictionary:dict] doubleValue];
            self.name = [self objectOrNilForKey:kSplashObjName fromDictionary:dict];
            self.icon = [self objectOrNilForKey:kSplashObjIcon fromDictionary:dict];
        CGFloat width = [NSString calculateTextWidth:30 Content:self.name font:themeFont13];//15
        self.nameWidth = width+20;
        
    NSObject *receivedSplashChild = [dict objectForKey:kSplashObjChild];
    NSMutableArray *parsedSplashChild = [NSMutableArray array];
    if ([receivedSplashChild isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSplashChild) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSplashChild addObject:[SplashChild modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSplashChild isKindOfClass:[NSDictionary class]]) {
       [parsedSplashChild addObject:[SplashChild modelObjectWithDictionary:(NSDictionary *)receivedSplashChild]];
    }

    self.child = [NSArray arrayWithArray:parsedSplashChild];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.internalBaseClassIdentifier forKey:kSplashObjId];
    [mutableDict setValue:[NSNumber numberWithDouble:self.level] forKey:kSplashObjLevel];
    [mutableDict setValue:self.name forKey:kSplashObjName];
    [mutableDict setValue:self.icon forKey:kSplashObjIcon];
    NSMutableArray *tempArrayForChild = [NSMutableArray array];
    for (NSObject *subArrayObject in self.child) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForChild addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForChild addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForChild] forKey:kSplashObjChild];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.internalBaseClassIdentifier = [aDecoder decodeObjectForKey:kSplashObjId];
    self.level = [aDecoder decodeDoubleForKey:kSplashObjLevel];
    self.name = [aDecoder decodeObjectForKey:kSplashObjName];
    self.icon = [aDecoder decodeObjectForKey:kSplashObjIcon];
    self.child = [aDecoder decodeObjectForKey:kSplashObjChild];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_internalBaseClassIdentifier forKey:kSplashObjId];
    [aCoder encodeDouble:_level forKey:kSplashObjLevel];
    [aCoder encodeObject:_name forKey:kSplashObjName];
    [aCoder encodeObject:_icon forKey:kSplashObjIcon];
    [aCoder encodeObject:_child forKey:kSplashObjChild];
}

- (id)copyWithZone:(NSZone *)zone
{
    SplashObj *copy = [[SplashObj alloc] init];
    
    if (copy) {

        copy.internalBaseClassIdentifier = [self.internalBaseClassIdentifier copyWithZone:zone];
        copy.level = self.level;
        copy.name = [self.name copyWithZone:zone];
        copy.icon = [self.icon copyWithZone:zone];
        copy.child = [self.child copyWithZone:zone];
    }
    
    return copy;
}


@end
